***** RISK stuff

use $pathdata/Risk/Risk_cleaned.dta, clear

keep if outlier == 0

** Compound figure
preserve
keep if rocl_set==1 & gains==1

egen bucket=group(rocl lottery_probability)
gen se_ce=.

forvalues i=1/14{
	qreg2 ce if bucket==`i', cluster(id)
	replace se_ce=_se[_cons] if bucket==`i'
}



collapse (p50) ce se_ce, by(lottery_probability rocl)

gen upper=ce+se_ce
gen lower=ce-se_ce

tw (scatter ce lottery_probability if rocl==0, msize(medsmall) ms(o) mcolor(midblue%90)) ///
	(scatter ce lottery_probability if rocl==1, msize(medium) ms(X) mcolor(red%80))  ///
 	(rcap upper lower lottery_probability if rocl==0, msiz(large) lcolor(midblue%90))	///
	(rcap upper lower lottery_probability  if rocl==1, msiz(large) lcolor(red%80))	///
	(function y=x , range(0 100) lpattern(dash) lcolor(black) lwidth(thin) ), ///
	ytitle("Normalized certainty equivalent") title("Compound choice under risk", color(black)) ///
    xtitle("Payoff probability") xscale( lcolor(none)) ysc(lcolor(none)) ///
	yline(0, lcolor(gs10) lwidth(thin)) ///
	graphregion(color(white)) ///
	ylabel(, tlc(none) angle(0) glcolor(gs15) glwidth(thin)) ///
	legend(order(1 2 3 5) label(1 "Baseline lottery") label(2 "Compound lottery") label(3 "{c 177}1 std. error of median") label(5 "Risk-neutral prediction") r(2))

restore

*** Plot of CU vs. p

preserve
keep if baseline_set == 1
keep if lottery_amount > 0

gen se_cu=.
tab lottery_probability
foreach i in 10 25 50 75 90 95 {
	qreg2 cognitive_uncertainty if lottery_probability==`i', cl(id)
	replace se_cu=_se[_cons] if lottery_probability==`i'
}

collapse (p50) cognitive_uncertainty  se_cu, by(lottery_probability)

gen upper=cognitive_uncertainty+se_cu
gen lower=cognitive_uncertainty-se_cu

tw 	(scatter cognitive_uncertainty lottery_probability, msize(medsmall)  ms(o) mcolor(black)) ///
	(rcap upper lower lottery_probability, msiz(large) lcolor(black)),	///
	ytitle("Cognitive Uncertainty") ///
    xtitle("Payoff probability") xscale( lcolor(none)) ysc(lcolor(none)) ///
	yline(0, lcolor(gs10) lwidth(thin)) ///
	graphregion(color(white)) ///
	ylabel(, tlc(none) angle(0) glcolor(gs15) glwidth(thin)) ///
	legend(order(1 2) label(1 "Cognitive uncertainty") label(2 "{c 177}1 std. error of median"))
	

restore



*******************************************
******       ARBITRARILIY HERE       ******

use $pathdata/Risk/Risk_cleaned.dta, clear

keep if outlier == 0

xtile cu_pct=cognitive_uncertainty, n(2)

sum cognitive_uncertainty if gains==1 & default_set == 1,d
gen low_cu=(cognitive_uncertainty<=r(p50))

gen default_coef=.

forvalues i=1/2{
	reg abs_ce low_default if gains==1 & default_set == 1 & cu_pct==`i', cl(id)
	estimates store risk`i'
}


use $pathdata/Beliefs/Beliefs_cleaned.dta, clear

keep if outlier == 0

xtile cu_pct=cognitive_uncertainty, n(2)

sum cognitive_uncertainty if default_set==1,d
gen low_cu=(cognitive_uncertainty<=r(p50))

gen default_coef=.

forvalues i=1/2{
	reg belief low_default if default_set==1 & cu_pct==`i', cl(id)
	estimates store beliefs`i'
}

coefplot (risk1, color(midblue%90) ciopts(color(midblue%90))) (risk2, mcolor(red%80)   ciopts(color(red%80)) lcolor(red%70)), ///
		ytitle("Treatment effect of low default") drop(_cons) yline(0, lcolor(grey) lpattern(dash)) vertical ysc(lcolor(none) r(-21, 1)) ylabel(-20 -15 -10 -5 0, tlc(none) angle(0) glcolor(gs15) glwidth(thin)) bgcolor(white) xla(, tlc(none) labcolor(none)) title("Choice under risk experiments", color(black)) ///
		xscale( lcolor(none)) ysc(lcolor(none)) ///
		yline(0, lcolor(gs10) lwidth(thin)) ///
		graphregion(color(white) fcolor(white)) ///
		ylabel(, tlc(none) angle(0) glcolor(gs15) glwidth(thin)) ///
		legend(order(2 4) label(2 "Below-median CU") label(4 "Above-median CU"))
graph export "$pathout/Risk/Figs/default_Risk.pdf", replace

coefplot (beliefs1, mcolor(midblue%90) ciopts(color(midblue%90))) (beliefs2, mcolor(red%80) lcolor(red%80)  ciopts(color(red%80))), ///
		ytitle("Treatment effect of low default") drop(_cons) yline(0, lcolor(grey) lpattern(dash)) vertical ysc(lcolor(none) r(-9,1)) ylabel(-8 -6 -4 -2 0) bgcolor(white) xla(, tlc(none) labcolor(none)) title("Belief updating experiments", color(black)) ///
		xscale( lcolor(none)) ysc(lcolor(none)) ///
		yline(0, lcolor(gs10) lwidth(thin)) ///
		graphregion(color(white)) ///
		ylabel(, tlc(none) angle(0) glcolor(gs15) glwidth(thin)) ///
		legend(order(2 4) label(2 "Below-median CU") label(4 "Above-median CU"))
		